{% extends 'admin/base.html' %}

{% block title %}编辑用户权限 - 管理后台{% endblock %}
{% block page_title %}编辑用户权限{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-pencil"></i> 编辑用户权限 - {{ target_user.user.username }}</h2>
            <a href="{% url 'bike:user_management' %}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> 返回用户列表
            </a>
        </div>

        <div class="card">
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="username" class="form-label">用户名</label>
                                <input type="text" class="form-control" id="username" name="username" 
                                       value="{{ target_user.user.username }}" required>
                                <small class="form-text text-muted">修改用户名</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="college" class="form-label">所属学院</label>
                                <select class="form-select" id="college" name="college" required>
                                    {% for value, label in college_choices %}
                                    <option value="{{ value }}" {% if target_user.college == value %}selected{% endif %}>{{ label }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="password" class="form-label">新密码</label>
                                <input type="password" class="form-control" id="password" name="password" 
                                       placeholder="留空表示不修改密码">
                                <small class="form-text text-muted">留空表示不修改密码</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">当前状态</label>
                                <div class="form-control-plaintext">
                                    {% if target_user.is_super_admin %}
                                        <span class="badge bg-danger">超级管理员</span>
                                    {% elif target_user.is_admin %}
                                        <span class="badge bg-warning">学院管理员</span>
                                    {% else %}
                                        <span class="badge bg-secondary">普通用户</span>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="is_admin" name="is_admin" 
                                           {% if target_user.is_admin %}checked{% endif %}>
                                    <label class="form-check-label" for="is_admin">
                                        学院管理员
                                    </label>
                                    <small class="form-text text-muted d-block">可以管理本学院的所有自行车</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="is_super_admin" name="is_super_admin" 
                                           {% if target_user.is_super_admin %}checked{% endif %}>
                                    <label class="form-check-label" for="is_super_admin">
                                        超级管理员
                                    </label>
                                    <small class="form-text text-muted d-block">可以管理所有学院和用户</small>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="alert alert-warning" role="alert">
                        <h6 class="alert-heading">权限说明</h6>
                        <ul class="mb-0">
                            <li><strong>学院管理员</strong>：可以管理本学院的所有自行车、查看违章记录、生成二维码</li>
                            <li><strong>超级管理员</strong>：拥有所有权限，包括用户管理、查看所有学院数据、导出二维码</li>
                        </ul>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle"></i> 保存更改
                        </button>
                        <a href="{% url 'bike:user_management' %}" class="btn btn-secondary">
                            <i class="bi bi-x-circle"></i> 取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
